package com.logic.landseaserver.ws;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.ws.dto.system.UserPassChangeDTO;
import com.logic.landseaserver.service.IUser;
import com.logic.landseaserver.ws.request.RegisterReq;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * Created by Drake on 2017/4/17.
 */
@Path("/wxUser")
@Api(value = "微信用户")
public class WxUserRest {
    @Autowired
    IUser userService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @POST
    @Path("/register")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Register a new customer based on the passed user object.", position = 1)
    public Response createCustomer(@ApiParam(value = "param", required = true) RegisterReq registerRsp) {
        CommonResultMap map = new CommonResultMap();
        try {
            synchronized (userService) {
                map.setData(userService.register(registerRsp));
            }
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("createCustomer error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("createCustomer error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Initialize Data", position = 6)
    public Response get() {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(userService.getInfo());
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("get error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("get error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/updatePwd")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "update user password", position = 12)
    public Response updateUserPwd(@ApiParam(value = "param", required = true) RegisterReq registerRsp) {
        CommonResultMap map = new CommonResultMap();
        try {

            userService.updateUserPwd(registerRsp);
            map.setResult(CommonResult.SUCCESS, "update success");
        } catch (BusinessException e) {
            logger.error("updateUserPwd error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("updateUserPwd error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @GET
    @Path("/myOpenId")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "update my openid ", position = 10)
    public Response updateUserOpenId(@ApiParam(value = "param", required = true) @QueryParam("code") String code, @ApiParam(value = "param", required = true) @QueryParam("userId") Integer userId) {
        CommonResultMap map = new CommonResultMap();
        try {
            userService.updateUserOpenId(code, userId);
            map.setResult(CommonResult.SUCCESS, "update success");
        } catch (Exception e) {
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @GET
    @Path("/myOpenId/{code}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "update my openid ", position = 10)
    public Response updateMyOpenId(@ApiParam(value = "param", required = true) @PathParam("code") String code) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(userService.getOpenId(code));
            map.setResult(CommonResult.SUCCESS, "update success");
        } catch (Exception e) {
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @PUT
    @Path("/changePassword")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Change Password By User ID")
    public Response updatePassword(@ApiParam(value = "param", required = true) UserPassChangeDTO userPassChangeDTO) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(userService.updatePassword(userPassChangeDTO));
            map.setResult(CommonResult.SUCCESS, "update success");
        } catch (Exception e) {
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
}
